

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>rl_coach.memories.non_episodic.differentiable_neural_dictionary &mdash; Reinforcement Learning Coach 0.12.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
    <link href="../../../../_static/css/custom.css" rel="stylesheet" type="text/css">

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> Reinforcement Learning Coach
          

          
            
            <img src="../../../../_static/dark_logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Intro</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../usage.html">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../dist_usage.html">Usage - Distributed Coach</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../features/index.html">Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../selecting_an_algorithm.html">Selecting an Algorithm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../dashboard.html">Coach Dashboard</a></li>
</ul>
<p class="caption"><span class="caption-text">Design</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/control_flow.html">Control Flow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/network.html">Network Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../design/horizontal_scaling.html">Distributed Coach - Horizontal Scale-Out</a></li>
</ul>
<p class="caption"><span class="caption-text">Contributing</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../contributing/add_agent.html">Adding a New Agent</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../contributing/add_env.html">Adding a New Environment</a></li>
</ul>
<p class="caption"><span class="caption-text">Components</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/agents/index.html">Agents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/architectures/index.html">Architectures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/data_stores/index.html">Data Stores</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/environments/index.html">Environments</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/exploration_policies/index.html">Exploration Policies</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/filters/index.html">Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/memories/index.html">Memories</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/memory_backends/index.html">Memory Backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/orchestrators/index.html">Orchestrators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/core_types.html">Core Types</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/spaces.html">Spaces</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../components/additional_parameters.html">Additional Parameters</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Reinforcement Learning Coach</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>rl_coach.memories.non_episodic.differentiable_neural_dictionary</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for rl_coach.memories.non_episodic.differentiable_neural_dictionary</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Copyright (c) 2017 Intel Corporation </span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pickle</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">annoy</span>
    <span class="kn">from</span> <span class="nn">annoy</span> <span class="k">import</span> <span class="n">AnnoyIndex</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">rl_coach.logger</span> <span class="k">import</span> <span class="n">failed_imports</span>
    <span class="n">failed_imports</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;annoy&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">AnnoyDictionary</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dict_size</span><span class="p">,</span> <span class="n">key_width</span><span class="p">,</span> <span class="n">new_value_shift_coefficient</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">key_error_threshold</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
                 <span class="n">num_neighbors</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">override_existing_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">rebuild_on_every_update</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rebuild_on_every_update</span> <span class="o">=</span> <span class="n">rebuild_on_every_update</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_size</span> <span class="o">=</span> <span class="n">dict_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_value_shift_coefficient</span> <span class="o">=</span> <span class="n">new_value_shift_coefficient</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_neighbors</span> <span class="o">=</span> <span class="n">num_neighbors</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">override_existing_keys</span> <span class="o">=</span> <span class="n">override_existing_keys</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">AnnoyIndex</span><span class="p">(</span><span class="n">key_width</span><span class="p">,</span> <span class="n">metric</span><span class="o">=</span><span class="s1">&#39;euclidean&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">set_seed</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">embeddings</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">dict_size</span><span class="p">,</span> <span class="n">key_width</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">dict_size</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">additional_data</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">dict_size</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lru_timestamps</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">dict_size</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="c1"># keys that are in this distance will be considered as the same key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key_error_threshold</span> <span class="o">=</span> <span class="n">key_error_threshold</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">initial_update_size</span> <span class="o">=</span> <span class="n">batch_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_update_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_update_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key_dimension</span> <span class="o">=</span> <span class="n">key_width</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">value_dimension</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reset_buffer</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">built_capacity</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">additional_data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">force_rebuild_tree</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">additional_data</span><span class="p">:</span>
            <span class="n">additional_data</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>

        <span class="c1"># Adds new embeddings and values to the dictionary</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">indices_to_remove</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">keys</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
            <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lookup_key_index</span><span class="p">(</span><span class="n">keys</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">index</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">override_existing_keys</span><span class="p">:</span>
                <span class="c1"># update existing value</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_value_shift_coefficient</span> <span class="o">*</span> <span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">index</span><span class="p">])</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">additional_data</span><span class="p">[</span><span class="n">index</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">additional_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">lru_timestamps</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span>
                <span class="n">indices_to_remove</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># add new</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_size</span><span class="p">:</span>
                    <span class="c1"># find the LRU entry</span>
                    <span class="n">index</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lru_timestamps</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">lru_timestamps</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span>
                <span class="n">indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">indices_to_remove</span><span class="p">):</span>
            <span class="n">keys</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
            <span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">additional_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_keys</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_keys</span><span class="p">,</span> <span class="n">keys</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_values</span><span class="p">,</span> <span class="n">values</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span> <span class="o">+</span> <span class="n">indices</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_additional_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffered_additional_data</span> <span class="o">+</span> <span class="n">additional_data</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_update_size</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_update_size</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">initial_update_size</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span> <span class="o">*</span> <span class="mf">0.02</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_rebuild_index</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">force_rebuild_tree</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">rebuild_on_every_update</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_rebuild_index</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="c1"># Returns the stored embeddings and values of the closest embeddings</span>
    <span class="k">def</span> <span class="nf">query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_enough_entries</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
            <span class="c1"># this will only happen when the DND is not yet populated with enough entries, which is only during heatup</span>
            <span class="c1"># these values won&#39;t be used and therefore they are meaningless</span>
            <span class="k">return</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">0.0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span>

        <span class="n">_</span><span class="p">,</span> <span class="n">indices</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_k_nearest_neighbors_indices</span><span class="p">(</span><span class="n">keys</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>

        <span class="n">embeddings</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">values</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">additional_data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lru_timestamps</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span>
            <span class="n">embeddings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">embeddings</span><span class="p">[</span><span class="n">ind</span><span class="p">])</span>
            <span class="n">values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">ind</span><span class="p">])</span>
            <span class="n">curr_additional_data</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">sub_ind</span> <span class="ow">in</span> <span class="n">ind</span><span class="p">:</span>
                <span class="n">curr_additional_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">additional_data</span><span class="p">[</span><span class="n">sub_ind</span><span class="p">])</span>
            <span class="n">additional_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">curr_additional_data</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">current_timestamp</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">embeddings</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">additional_data</span>

    <span class="k">def</span> <span class="nf">has_enough_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span> <span class="o">&gt;</span> <span class="n">k</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">built_capacity</span> <span class="o">&gt;</span> <span class="n">k</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">sample_embeddings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_embeddings</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">embeddings</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span><span class="p">,</span> <span class="n">num_embeddings</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_get_k_nearest_neighbors_indices</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="n">distances</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
            <span class="n">index</span><span class="p">,</span> <span class="n">distance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">get_nns_by_vector</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">include_distances</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">distances</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">distance</span><span class="p">)</span>
            <span class="n">indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">distances</span><span class="p">,</span> <span class="n">indices</span>

    <span class="k">def</span> <span class="nf">_rebuild_index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">unbuild</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">embeddings</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffered_keys</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_values</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffered_additional_data</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">additional_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">buffered_keys</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">add_item</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_reset_buffer</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_neighbors</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">built_capacity</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">curr_size</span>

    <span class="k">def</span> <span class="nf">_reset_buffer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_keys</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">key_dimension</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_dimension</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">buffered_additional_data</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">_lookup_key_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="n">distance</span><span class="p">,</span> <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_k_nearest_neighbors_indices</span><span class="p">([</span><span class="n">key</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">distance</span> <span class="o">!=</span> <span class="p">[[]]</span> <span class="ow">and</span> <span class="n">distance</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">key_error_threshold</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">index</span>
        <span class="k">return</span> <span class="kc">None</span>


<div class="viewcode-block" id="QDND"><a class="viewcode-back" href="../../../../components/memories/index.html#rl_coach.memories.non_episodic.QDND">[docs]</a><span class="k">class</span> <span class="nc">QDND</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dict_size</span><span class="p">,</span> <span class="n">key_width</span><span class="p">,</span> <span class="n">num_actions</span><span class="p">,</span> <span class="n">new_value_shift_coefficient</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">key_error_threshold</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span>
                 <span class="n">learning_rate</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">num_neighbors</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">return_additional_data</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">override_existing_keys</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">rebuild_on_every_update</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dict_size</span> <span class="o">=</span> <span class="n">dict_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key_width</span> <span class="o">=</span> <span class="n">key_width</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_actions</span> <span class="o">=</span> <span class="n">num_actions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_value_shift_coefficient</span> <span class="o">=</span> <span class="n">new_value_shift_coefficient</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key_error_threshold</span> <span class="o">=</span> <span class="n">key_error_threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span> <span class="o">=</span> <span class="n">learning_rate</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_neighbors</span> <span class="o">=</span> <span class="n">num_neighbors</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">return_additional_data</span> <span class="o">=</span> <span class="n">return_additional_data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">override_existing_keys</span> <span class="o">=</span> <span class="n">override_existing_keys</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># create a dict for each action</span>
        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="n">new_dict</span> <span class="o">=</span> <span class="n">AnnoyDictionary</span><span class="p">(</span><span class="n">dict_size</span><span class="p">,</span> <span class="n">key_width</span><span class="p">,</span> <span class="n">new_value_shift_coefficient</span><span class="p">,</span>
                                       <span class="n">key_error_threshold</span><span class="o">=</span><span class="n">key_error_threshold</span><span class="p">,</span> <span class="n">num_neighbors</span><span class="o">=</span><span class="n">num_neighbors</span><span class="p">,</span>
                                       <span class="n">override_existing_keys</span><span class="o">=</span><span class="n">override_existing_keys</span><span class="p">,</span>
                                       <span class="n">rebuild_on_every_update</span><span class="o">=</span><span class="n">rebuild_on_every_update</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_dict</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">embeddings</span><span class="p">,</span> <span class="n">actions</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">additional_data</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># add a new set of embeddings and values to each of the underlining dictionaries</span>
        <span class="n">embeddings</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">embeddings</span><span class="p">)</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">actions</span><span class="p">)</span>
        <span class="n">values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="n">idx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">actions</span> <span class="o">==</span> <span class="n">a</span><span class="p">)</span>
            <span class="n">curr_action_embeddings</span> <span class="o">=</span> <span class="n">embeddings</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
            <span class="n">curr_action_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">additional_data</span><span class="p">:</span>
                <span class="n">curr_additional_data</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">idx</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                    <span class="n">curr_additional_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">additional_data</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">curr_additional_data</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">curr_action_embeddings</span><span class="p">,</span> <span class="n">curr_action_values</span><span class="p">,</span> <span class="n">curr_additional_data</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">embeddings</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="c1"># query for nearest neighbors to the given embeddings</span>
        <span class="n">dnd_embeddings</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">dnd_values</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">dnd_indices</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">dnd_additional_data</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">embeddings</span><span class="p">)):</span>
            <span class="n">embedding</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">indices</span><span class="p">,</span> <span class="n">additional_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">action</span><span class="p">]</span><span class="o">.</span><span class="n">query</span><span class="p">([</span><span class="n">embeddings</span><span class="p">[</span><span class="n">i</span><span class="p">]],</span> <span class="n">k</span><span class="p">)</span>
            <span class="n">dnd_embeddings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">embedding</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">dnd_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">dnd_indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">dnd_additional_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">additional_data</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_additional_data</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">dnd_embeddings</span><span class="p">,</span> <span class="n">dnd_values</span><span class="p">,</span> <span class="n">dnd_indices</span><span class="p">,</span> <span class="n">dnd_additional_data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">dnd_embeddings</span><span class="p">,</span> <span class="n">dnd_values</span><span class="p">,</span> <span class="n">dnd_indices</span>

    <span class="k">def</span> <span class="nf">has_enough_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="c1"># check if each of the action dictionaries has at least k entries</span>
        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">has_enough_entries</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">update_keys_and_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">actions</span><span class="p">,</span> <span class="n">key_gradients</span><span class="p">,</span> <span class="n">value_gradients</span><span class="p">,</span> <span class="n">indices</span><span class="p">):</span>
        <span class="c1"># Update DND keys and values</span>
        <span class="k">for</span> <span class="n">batch_action</span><span class="p">,</span> <span class="n">batch_keys</span><span class="p">,</span> <span class="n">batch_values</span><span class="p">,</span> <span class="n">batch_indices</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">actions</span><span class="p">,</span> <span class="n">key_gradients</span><span class="p">,</span> <span class="n">value_gradients</span><span class="p">,</span> <span class="n">indices</span><span class="p">):</span>
            <span class="c1"># Update keys (embeddings) and values in DND</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">batch_indices</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">batch_action</span><span class="p">]</span><span class="o">.</span><span class="n">embeddings</span><span class="p">[</span><span class="n">index</span><span class="p">,</span> <span class="p">:]</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span> <span class="o">*</span> <span class="n">batch_keys</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">batch_action</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">-=</span> <span class="bp">self</span><span class="o">.</span><span class="n">learning_rate</span> <span class="o">*</span> <span class="n">batch_values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">sample_embeddings</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_embeddings</span><span class="p">):</span>
        <span class="n">num_actions</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">)</span>
        <span class="n">embeddings</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">num_embeddings_per_action</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num_embeddings</span><span class="o">/</span><span class="n">num_actions</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">action</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="n">embeddings</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">action</span><span class="p">]</span><span class="o">.</span><span class="n">sample_embeddings</span><span class="p">(</span><span class="n">num_embeddings_per_action</span><span class="p">))</span>
        <span class="n">embeddings</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">(</span><span class="n">embeddings</span><span class="p">)</span>

        <span class="c1"># the numbers did not divide nicely, let&#39;s just randomly sample some more embeddings</span>
        <span class="k">if</span> <span class="n">num_embeddings_per_action</span> <span class="o">*</span> <span class="n">num_actions</span> <span class="o">&lt;</span> <span class="n">num_embeddings</span><span class="p">:</span>
            <span class="n">action</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">num_actions</span><span class="p">)</span>
            <span class="n">extra_embeddings</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">action</span><span class="p">]</span><span class="o">.</span><span class="n">sample_embeddings</span><span class="p">(</span><span class="n">num_embeddings</span> <span class="o">-</span>
                                                                   <span class="n">num_embeddings_per_action</span> <span class="o">*</span> <span class="n">num_actions</span><span class="p">)</span>
            <span class="n">embeddings</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">embeddings</span><span class="p">,</span> <span class="n">extra_embeddings</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">embeddings</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># create a new dict for each action</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="n">new_dict</span> <span class="o">=</span> <span class="n">AnnoyDictionary</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dict_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">key_width</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_value_shift_coefficient</span><span class="p">,</span>
                                       <span class="n">key_error_threshold</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">key_error_threshold</span><span class="p">,</span> <span class="n">num_neighbors</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">num_neighbors</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dicts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_dict</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">load_dnd</span><span class="p">(</span><span class="n">model_dir</span><span class="p">):</span>
    <span class="n">latest_checkpoint_id</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">latest_checkpoint</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="c1"># get all checkpoint files</span>
    <span class="k">for</span> <span class="n">fname</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">model_dir</span><span class="p">):</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">model_dir</span><span class="p">,</span> <span class="n">fname</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="ow">or</span> <span class="n">fname</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;srs&#39;</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">checkpoint_id</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fname</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">checkpoint_id</span> <span class="o">&gt;</span> <span class="n">latest_checkpoint_id</span><span class="p">:</span>
            <span class="n">latest_checkpoint</span> <span class="o">=</span> <span class="n">fname</span>
            <span class="n">latest_checkpoint_id</span> <span class="o">=</span> <span class="n">checkpoint_id</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">model_dir</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">latest_checkpoint</span><span class="p">)),</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">DND</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">DND</span><span class="o">.</span><span class="n">num_actions</span><span class="p">):</span>
            <span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">AnnoyIndex</span><span class="p">(</span><span class="mi">512</span><span class="p">,</span> <span class="n">metric</span><span class="o">=</span><span class="s1">&#39;euclidean&#39;</span><span class="p">)</span>
            <span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">set_seed</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">curr_size</span><span class="p">),</span> <span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">embeddings</span><span class="p">[:</span><span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">curr_size</span><span class="p">]):</span>
                <span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">add_item</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>

            <span class="n">DND</span><span class="o">.</span><span class="n">dicts</span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">DND</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018-2019, Intel AI Lab

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>